﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Text.RegularExpressions;
using System.IO;

using PlanningGIS.Framework;
using PlanningGIS.Framework.Config;

namespace PlanningGIS.Tools
{
    /// <summary>
    /// 比例尺设置界面
    /// </summary>
    public partial class FrmScaleSetting : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmScaleSetting()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmScaleSetting_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(AppConfig.SystemConfig.基本信息.默认比例尺))
            {
                lstScale.Items.Add("1:500");
                lstScale.Items.Add("1:2000");
                lstScale.Items.Add("1:10000");
                lstScale.Items.Add("1:50000");
                lstScale.Items.Add("1:250000");
                lstScale.Items.Add("1:1000000");
            }
            else
            {
                btnLoadDefault_Click(null, null);
            }
        }
        /// <summary>
        /// 当前比例尺
        /// </summary>
        public string CurrentScale { get; set; }
        /// <summary>
        /// 是否锁定缩放的比例尺
        /// </summary>
        public bool IsLock
        {
            get { return chkLock.Checked; }
            set { chkLock.Checked = value; }
        }
        /// <summary>
        /// 获取或设置比例尺
        /// </summary>
        public List<string> Values
        {
            get
            {
                if (lstScale.Items.Count == 0)
                    return null;
                List<string> vs = new List<string>();
                for (int i = 0; i < lstScale.Items.Count; i++)
                {
                    vs.Add(lstScale.Items[i].ToString());
                }
                return vs;
            }
            set
            {
                if (value != null && value.Count > 0)
                {
                    lstScale.Items.Clear();
                    for (int i = 0; i < value.Count; i++)
                    {
                        lstScale.Items.Add(value[i]);
                    }
                }
            }
        }
        /// <summary>
        /// 添加设置的比例尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtScale.Text))
            {
                AddScale(txtScale.Text.Trim());
                txtScale.Text = "";
            }
        }
        /// <summary>
        /// 添加当前比例尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCurrent_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(CurrentScale))
            {
                AddScale(CurrentScale.Trim());
            }
        }
        /// <summary>
        /// 设置比例尺
        /// </summary>
        /// <param name="scale"></param>
        private void SetScale(string scale)
        {
            if (string.IsNullOrEmpty(scale))
                return;
            lstScale.Items.Clear();
            string[] sc = scale.Split(';');
            foreach (string itm in sc)
            {
                AddScale(itm);
            }
        }
        /// <summary>
        /// 添加比例尺
        /// </summary>
        /// <param name="scale"></param>
        private void AddScale(string scale)
        {
            if (string.IsNullOrEmpty(scale))
                return;
            double dscale = 0;
            if (scale.Contains(":"))
            {
                string sv = scale.Split(':')[1];
                dscale = double.Parse(sv);
            }
            else
            {
                dscale = double.Parse(scale);
            }
            int cnt = lstScale.Items.Count;
            if (cnt == 0)
                lstScale.Items.Add("1:" + dscale.ToString());
            else
            {
                for (int i = cnt - 1; i >= 0; i--)
                {
                    string sv = lstScale.Items[i].ToString().Split(':')[1];
                    double d = double.Parse(sv);
                    if (dscale > d)
                    {
                        lstScale.Items.Insert(i + 1, "1:" + dscale.ToString());
                        break;
                    }
                    else if (dscale == d)
                    {
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 删除比例尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lstScale.Items.Count == 0 || lstScale.SelectedItem == null)
                return;
            lstScale.Items.Remove(lstScale.SelectedItem);
        }
        /// <summary>
        /// 删除所有比例尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelAll_Click(object sender, EventArgs e)
        {
            if (lstScale.Items.Count == 0)
                return;
            lstScale.Items.Clear();
        }
        /// <summary>
        /// 加载默认比例尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoadDefault_Click(object sender, EventArgs e)
        {
            string s = AppConfig.SystemConfig.基本信息.默认比例尺;
            if (string.IsNullOrEmpty(s))
                return;
            SetScale(s);
        }
        /// <summary>
        /// 从文件导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImpFile_Click(object sender, EventArgs e)
        {
            if (opnfidlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                string f = opnfidlg.FileName;
                System.Text.Encoding encode = PlanningGIS.Util.IO.FileHelper.GetFileEncodeType(f);
                if (encode != System.Text.Encoding.Unicode)
                    encode = System.Text.Encoding.Default;
                string s = PlanningGIS.Util.IO.FileHelper.ReadFile(f, encode);
                if (string.IsNullOrEmpty(s))
                    return;
                s = s.Replace("\0", "");
                s = s.Replace("\r", ";");
                SetScale(s);
            }
        }
        /// <summary>
        /// 保存当前的比例设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (lstScale.Items.Count == 0)
                return;
            if (svfidlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                string f = svfidlg.FileName;
                if (File.Exists(f))
                {
                    File.Delete(f);
                }
                string s = GetScaleString();
                s = s.Replace(";", "\r");
                PlanningGIS.Util.IO.FileHelper.WriteFile(f, s, System.Text.Encoding.Unicode);
            }
        }
        /// <summary>
        /// 设置为默认比例尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetDefault_Click(object sender, EventArgs e)
        {
            string s = GetScaleString();
            if (string.IsNullOrEmpty(s))
                return;
            AppConfig.SystemConfig.基本信息.默认比例尺 = s;
            ConfigHelper.SaveObject<PlanningGIS.Framework.Config.SystemConfig>(AppConfig.SystemConfig);
        }
        /// <summary>
        /// 获取当前设置的比例尺
        /// </summary>
        /// <returns></returns>
        private string GetScaleString()
        {
            if (lstScale.Items.Count == 0)
                return "";
            string s = "";
            for (int i = 0; i < lstScale.Items.Count; i++)
            {
                s += lstScale.Items[i].ToString().Replace("1:", "") + ";";
            }
            return s.Trim().Substring(0, s.Trim().Length - 1);
        }
        /// <summary>
        /// 确认设置，并关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (lstScale.Items.Count == 0)
                return;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }

}
